package com.owc.domain.vo.admin;

import com.owc.common.converter.InputConverter;
import com.owc.common.enums.ClientEnum;
import com.owc.common.enums.ExceptionEnum;
import com.owc.common.exception.BadRequestException;
import com.owc.common.tool.DateUtils;
import com.owc.domain.entity.Version;
import com.owc.filter.content.SecurityContextHolder;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Optional;

/**
 * @Author Jackies
 * @Date 2019/12/18 22:48
 * @Description TODO://
 **/
@Data
public class VersionVo implements InputConverter<Version> {

	@NotNull(message = "版本号不能是空")
	private String version;

	@NotNull(message = "请选择更新类型")
	private Integer type;

	@NotNull(message = "请输入更新提示语")
	private String notice;

	@NotNull(message = "请选择系统类型")
	private String system;

	@NotNull(message = "请输入下载地址")
	private String download;

	private Long id;

	@Override
	public void customConverter(Version version) throws Exception {
		version.setContent(notice);
		version.setCreateId(SecurityContextHolder.getCurrentUserId());
		version.setCreateTime(DateUtils.formatYMDHMS(new Date()));

		Optional<ClientEnum> enumOptional = ClientEnum.matchByDesc(system);
		if(!enumOptional.isPresent()){
			throw new BadRequestException(ExceptionEnum.NOT_FOUND_VERSION_TYPE);
		}
		version.setSystemType(enumOptional.get().getType());
		version.setForce(type);
		version.setCreateTime(DateUtils.formatYMDHMS(new Date()));
	}
}
